﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.huigo.Framework.ServiceUtils;
using System.Text;
using com.loto.Common;

namespace com.huigo.Admin.Web.customer
{
    public partial class CustList : BasePage
    {
        private int pageIndex
        {
            get
            {
                return com.loto.Common.TypeParse.StrToInt(Request.QueryString["page"], 1);
            }
        }

        private string queryCust
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["cust"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["cust"]);
            }
        }
        private DateTime queryStart
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["Start"]) ? Convert.ToDateTime(Request.QueryString["Start"]) : DateTime.MinValue;
            }
        }
        private DateTime queryEnd
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["End"]) ? Convert.ToDateTime(Request.QueryString["End"]) : DateTime.MinValue;
            }
        }
        private string queryType
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["Type"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["Type"]);
            }
        }
        private string UserRealName
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["userName"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["userName"]);
            }
        } 
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void pageInfo_PageChanged(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindType();
                BindData();
            }
               
           
        }

        protected void BindData()
        {
            custName.Text = queryCust;
            userName.Text = UserRealName;
            if (queryStart.ToString("yyyy-MM-dd") != string.Empty && queryEnd.ToString("yyyy-MM-dd") != string.Empty)
            {
                if (queryStart.ToString("yyyy-MM-dd") != "0001-01-01" && queryEnd.AddDays(1).ToString("yyyy-MM-dd") != "0001-01-02")
                {
                    beginDate.Text = queryStart.ToString("yyyy-MM-dd");
                    endDate.Text = queryEnd.ToString("yyyy-MM-dd");
                }
            }
            statusList.Text = queryType;
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            argList.Add(new RequestFuncParams("custName", queryCust));
            argList.Add(new RequestFuncParams("pageSize", pageInfo.PageSize.ToString()));
            argList.Add(new RequestFuncParams("pageIndex", pageIndex.ToString()));
            argList.Add(new RequestFuncParams("start", queryStart.ToString()));
            argList.Add(new RequestFuncParams("end", queryEnd.ToString()));
            argList.Add(new RequestFuncParams("type", queryType));
            argList.Add(new RequestFuncParams("userName", UserRealName));
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_CUST_LIST, argList);

            if (parser != null && parser.RowsCount > 0)
            {
                pageInfo.RecordCount = Convert.ToInt16(parser["RecordCount", 0]);
                pageInfo.CurrentPageIndex = pageIndex;
                pageInfo.PageURLFormat = GetPagerUrl();
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("<table>");
                sb.AppendLine("<thead>");
                sb.AppendLine("<tr>");
                sb.AppendLine("<th width=\"10%\">");
                sb.AppendLine("经销商名称");
                sb.AppendLine("</th>");
                sb.AppendLine("<th width=\"15%\">");
                sb.AppendLine("EMAIL");
                sb.AppendLine("</th>");
                sb.AppendLine("<th>");
                sb.AppendLine("类型");
                sb.AppendLine("</th>");
                sb.AppendLine("<th>");
                sb.AppendLine("级别");
                sb.AppendLine("</th>");
                sb.AppendLine("<th>");
                sb.AppendLine("余额");
                sb.AppendLine("</th>");
                sb.AppendLine("<th>");
                sb.AppendLine("可用余额");
                sb.AppendLine("</th>");
                sb.AppendLine("<th>");
                sb.AppendLine("积分");
                sb.AppendLine("</th>");
                sb.AppendLine("<th>");
                sb.AppendLine("可用积分");
                sb.AppendLine("</th>");
                sb.AppendLine("<th width=\"12%\">");
                sb.AppendLine("创建时间");
                sb.AppendLine("</th>");
                sb.AppendLine("<th width=\"12%\">");
                sb.AppendLine("最后登录时间");
                sb.AppendLine("</th>");
                sb.AppendLine("<th>");
                sb.AppendLine("状态");
                sb.AppendLine("</th>");
                sb.AppendLine("<th>");
                sb.AppendLine("标识");
                sb.AppendLine("</th>");
                sb.AppendLine("<th>");
                sb.AppendLine("销售员");
                sb.AppendLine("</th>");
                sb.AppendLine("<th>");
                sb.AppendLine("操作");
                sb.AppendLine("</th>");
                sb.AppendLine("</tr>");
                sb.AppendLine("</thead>");
                sb.AppendLine("<tbody>");

                for (int i = 0; i < parser.RowsCount; i++)
                {
                    sb.AppendLine("<tr>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(parser["CustName", i]);
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(parser["CustEmail", i]);
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(parser["TypeName", i]);
                    sb.AppendLine("</td>");
                    //sb.AppendLine("<td>");
                    //sb.AppendLine(com.loto.Common.DictEnum.GetDictValueByKey(Dict.DictCustSource, Utils.StrToInt(parser["CustFrom", i], int.MinValue)));
                    //sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(parser["LevelName", i]);
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(string.Format("{0:C2}", Convert.ToDecimal(parser["CustTotalFee", i])));
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(string.Format("{0:C2}", Convert.ToDecimal(parser["CustUsefulFee", i])));
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(parser["CustTotalPoint", i]);
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(parser["CustUsefulPoint", i]);
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(Convert.ToDateTime(parser["CustCreatedOn", i]).ToString("yyyy-MM-dd HH:mm"));
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(Convert.ToDateTime(parser["CustLastActivedOn", i]).ToString("yyyy-MM-dd HH:mm"));
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(com.loto.Common.DictEnum.GetDictValueByKey(Dict.DictCustStatus, Utils.StrToInt(parser["CustStatus", i], int.MinValue)));
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(parser["CustMark", i]);
                    sb.AppendLine("</td>");
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(parser["UserRealName", i]);
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine("<a href=\"AddCust.aspx?cid=" + parser["CustId", i] + "&keepThis=true&amp;TB_iframe=true&amp;height=500&amp;width=850\" title=\"修改经销商-" + parser["CustName", i] + "\" class=\"thickbox\">修改</a>");
                    sb.AppendLine("</td>");
                    sb.AppendLine("</tr>");
                }
                sb.AppendLine("</tbody>");
                sb.AppendLine("</table>");
                litCust.Text = sb.ToString();
            }
            else
                litCust.Text = string.Empty;


        }
        protected void BindType()
        {
            List<DropList> statusDropList = Tools.DictToDropList(Dict.DictCustStatus);
            if (statusDropList != null)
            {
                statusList.DataSource = statusDropList;
                statusList.DataValueField = "Id";
                statusList.DataTextField = "Value";
                statusList.DataBind();
            };
        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string url = "CustList.aspx?";
            url += "page=1";
            if (custName.Text != string.Empty)
                url += "&cust=" + custName.Text;
            if (beginDate.Text != string.Empty)
                url += "&Start="  +beginDate.Text;
            if (endDate.Text != string.Empty)
                url += "&End=" + endDate.Text;
            if (statusList.SelectedValue != string.Empty)
                url += "&Type=" + statusList.SelectedValue;
            if (userName.Text != string.Empty)
                url += "&userName=" + userName.Text;
            if (custName.Text == string.Empty && beginDate.Text == string.Empty && endDate.Text == string.Empty && statusList.SelectedValue == string.Empty && userName.Text == string.Empty)
            {
                url = "CustList.aspx";
            }
            Response.Redirect(url);
        }

        private string GetPagerUrl()
        {
            string url = "CustList.aspx?";
            url += "page={0}";
            if (queryCust != string.Empty)
                url += "&cust=" + queryCust;
            if (queryStart != DateTime.MinValue)
                url += "&Start=" + queryStart.ToString("yyyy-MM-dd");
            if (queryEnd != DateTime.MinValue)
                url += "&End=" + queryEnd.ToString("yyyy-MM-dd");
            if (queryType != string.Empty)
                url += "&Type=" + queryType;
            if (userName.Text != string.Empty)
                url += "&userName=" + UserRealName;
            return url;
        }

    }
}